/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本系统已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2023 天天 all rights reserved.
 * 版权所有，侵权必究！
 */
package ltd.tiantian.mall.service.impl;

import ltd.tiantian.mall.common.Constants;
import ltd.tiantian.mall.common.ServiceResultEnum;
import ltd.tiantian.mall.controller.vo.TianTianMallShoppingCartItemVO;
import ltd.tiantian.mall.dao.TianTianMallGoodsMapper;
import ltd.tiantian.mall.dao.TianTianMallShoppingCartItemMapper;
import ltd.tiantian.mall.entity.TianTianMallGoods;
import ltd.tiantian.mall.entity.TianTianMallShoppingCartItem;
import ltd.tiantian.mall.service.TianTianMallShoppingCartService;
import ltd.tiantian.mall.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
public class TianTianMallShoppingCartServiceImpl implements TianTianMallShoppingCartService {

    @Autowired
    private TianTianMallShoppingCartItemMapper tianTianMallShoppingCartItemMapper;

    @Autowired
    private TianTianMallGoodsMapper tianTianMallGoodsMapper;

    @Override
    public String saveTianTianMallCartItem(TianTianMallShoppingCartItem tianTianMallShoppingCartItem) {
        TianTianMallShoppingCartItem temp = tianTianMallShoppingCartItemMapper.selectByUserIdAndGoodsId(tianTianMallShoppingCartItem.getUserId(), tianTianMallShoppingCartItem.getGoodsId());
        if (temp != null) {
            //已存在则修改该记录
            temp.setGoodsCount(tianTianMallShoppingCartItem.getGoodsCount());
            return updateTianTianMallCartItem(temp);
        }
        TianTianMallGoods tianTianMallGoods = tianTianMallGoodsMapper.selectByPrimaryKey(tianTianMallShoppingCartItem.getGoodsId());
        //商品为空
        if (tianTianMallGoods == null) {
            return ServiceResultEnum.GOODS_NOT_EXIST.getResult();
        }
        int totalItem = tianTianMallShoppingCartItemMapper.selectCountByUserId(tianTianMallShoppingCartItem.getUserId()) + 1;
        //超出单个商品的最大数量
        if (tianTianMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        //超出最大数量
        if (totalItem > Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_TOTAL_NUMBER_ERROR.getResult();
        }
        //保存记录
        if (tianTianMallShoppingCartItemMapper.insertSelective(tianTianMallShoppingCartItem) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateTianTianMallCartItem(TianTianMallShoppingCartItem tianTianMallShoppingCartItem) {
        TianTianMallShoppingCartItem tianTianMallShoppingCartItemUpdate = tianTianMallShoppingCartItemMapper.selectByPrimaryKey(tianTianMallShoppingCartItem.getCartItemId());
        if (tianTianMallShoppingCartItemUpdate == null) {
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        //超出单个商品的最大数量
        if (tianTianMallShoppingCartItem.getGoodsCount() > Constants.SHOPPING_CART_ITEM_LIMIT_NUMBER) {
            return ServiceResultEnum.SHOPPING_CART_ITEM_LIMIT_NUMBER_ERROR.getResult();
        }
        // 数量相同不会进行修改
        if (tianTianMallShoppingCartItemUpdate.getGoodsCount().equals(tianTianMallShoppingCartItem.getGoodsCount())) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        // userId不同不能修改
        if (!tianTianMallShoppingCartItem.getUserId().equals(tianTianMallShoppingCartItemUpdate.getUserId())) {
            return ServiceResultEnum.NO_PERMISSION_ERROR.getResult();
        }
        tianTianMallShoppingCartItemUpdate.setGoodsCount(tianTianMallShoppingCartItem.getGoodsCount());
        tianTianMallShoppingCartItemUpdate.setUpdateTime(new Date());
        //修改记录
        if (tianTianMallShoppingCartItemMapper.updateByPrimaryKeySelective(tianTianMallShoppingCartItemUpdate) > 0) {
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public TianTianMallShoppingCartItem getTianTianMallCartItemById(Long tianTianMallShoppingCartItemId) {
        return tianTianMallShoppingCartItemMapper.selectByPrimaryKey(tianTianMallShoppingCartItemId);
    }

    @Override
    public Boolean deleteById(Long shoppingCartItemId, Long userId) {
        TianTianMallShoppingCartItem tianTianMallShoppingCartItem = tianTianMallShoppingCartItemMapper.selectByPrimaryKey(shoppingCartItemId);
        if (tianTianMallShoppingCartItem == null) {
            return false;
        }
        //userId不同不能删除
        if (!userId.equals(tianTianMallShoppingCartItem.getUserId())) {
            return false;
        }
        return tianTianMallShoppingCartItemMapper.deleteByPrimaryKey(shoppingCartItemId) > 0;
    }

    @Override
    public List<TianTianMallShoppingCartItemVO> getMyShoppingCartItems(Long tianTianMallUserId) {
        List<TianTianMallShoppingCartItemVO> tianTianMallShoppingCartItemVOS = new ArrayList<>();
        List<TianTianMallShoppingCartItem> tianTianMallShoppingCartItems = tianTianMallShoppingCartItemMapper.selectByUserId(tianTianMallUserId, Constants.SHOPPING_CART_ITEM_TOTAL_NUMBER);
        if (!CollectionUtils.isEmpty(tianTianMallShoppingCartItems)) {
            //查询商品信息并做数据转换
            List<Long> tianTianMallGoodsIds = tianTianMallShoppingCartItems.stream().map(TianTianMallShoppingCartItem::getGoodsId).collect(Collectors.toList());
            List<TianTianMallGoods> tianTianMallGoods = tianTianMallGoodsMapper.selectByPrimaryKeys(tianTianMallGoodsIds);
            Map<Long, TianTianMallGoods> tianTianMallGoodsMap = new HashMap<>();
            if (!CollectionUtils.isEmpty(tianTianMallGoods)) {
                tianTianMallGoodsMap = tianTianMallGoods.stream().collect(Collectors.toMap(TianTianMallGoods::getGoodsId, Function.identity(), (entity1, entity2) -> entity1));
            }
            for (TianTianMallShoppingCartItem tianTianMallShoppingCartItem : tianTianMallShoppingCartItems) {
                TianTianMallShoppingCartItemVO tianTianMallShoppingCartItemVO = new TianTianMallShoppingCartItemVO();
                BeanUtil.copyProperties(tianTianMallShoppingCartItem, tianTianMallShoppingCartItemVO);
                if (tianTianMallGoodsMap.containsKey(tianTianMallShoppingCartItem.getGoodsId())) {
                    TianTianMallGoods tianTianMallGoodsTemp = tianTianMallGoodsMap.get(tianTianMallShoppingCartItem.getGoodsId());
                    tianTianMallShoppingCartItemVO.setGoodsCoverImg(tianTianMallGoodsTemp.getGoodsCoverImg());
                    String goodsName = tianTianMallGoodsTemp.getGoodsName();
                    // 字符串过长导致文字超出的问题
                    if (goodsName.length() > 28) {
                        goodsName = goodsName.substring(0, 28) + "...";
                    }
                    tianTianMallShoppingCartItemVO.setGoodsName(goodsName);
                    tianTianMallShoppingCartItemVO.setSellingPrice(tianTianMallGoodsTemp.getSellingPrice());
                    tianTianMallShoppingCartItemVOS.add(tianTianMallShoppingCartItemVO);
                }
            }
        }
        return tianTianMallShoppingCartItemVOS;
    }
}
